import { Controller, Get, Param } from '@nestjs/common';
import { BaseController } from '../../base/base.controller';
import { DbService } from '../../db/db.service';
import { DictionaryItem } from '@app/db/common/entities/DictionaryItem';

@Controller('dictionary')
export class DictionaryController extends BaseController{
    constructor(
        private readonly db:DbService
    ){
        super()
    }

    @Get('list')
    async list() {
        const categorys = await this.db.dictionaryCategory.find()
        const items = await this.db.dictionaryItem.find()
        return this.successRes({categorys : categorys, items : items})
    }

    @Get('item/:categoryCode')
    async getItemsByCategory(@Param('categoryCode') categoryCode:string) {
        let items:DictionaryItem[] = []
        const category = await this.db.dictionaryCategory.findOne({code : categoryCode}, ['id'])
        if(category) {
            items = await this.db.dictionaryItem.find({categoryId : category.id}, ['name', 'code', 'id', 'parentId'])
        }

        return this.successRes(items)

    }
}
